<?php
	$labelWidth = ($labelWidth ? ' style="width: ' . $labelWidth . '"' : $labelWidth);
	$action = (isset($action) ? $action : true);
	if ($action === 'begin') {
		echo '<form' . Html::attributes($attributes) . '>';
		foreach($elements as $el) {
			if ($el instanceof Form_Field_Hidden) {
				echo $el->render() . chr(10);
			}
		}
		if  ($legend) {
			echo '<table><tr><td ' . $labelWidth . ' colspan="2">' . $legend . '</td></tr></table>' . chr(10);
		}
	}

	if ($action === 'fieldset' || $action === true) {
//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------
foreach($elements as $el) {
	if ($action === 'fieldset') {
		if ($el->fieldset !== $fieldset) {
			continue;
		}
	}
	if ($el->rendered) {
		continue;
	}
	$fieldsetElements = array();
	if ($el->fieldset) {
		echo '<fieldset' . Html::attributes($fieldsets[$el->fieldset]['attributes']) . '>' . chr(10);
		if (!empty($fieldsets[$fieldset]['legend'])) {
			echo '<legend>' . Html::chars($fieldsets[$el->fieldset]['legend']) . '</legend>';
		}
		foreach ($fieldsets[$el->fieldset]['elements'] as $element) {
			$fieldsetElements[] = $elements[$element];
		}
	} else {
		foreach($elements as $falseElement) {
			if ($falseElement->fieldset === false) {
				$fieldsetElements[] = $falseElement;
			}
		}
	}
	echo '<table>';
	$cnt = 0;
	foreach ($fieldsetElements as $element) {
		switch (get_class($element)) {
			case 'Form_Field_Hidden':
				break;
			case 'Form_Field_Comment':
				echo $element->render();
				break;
			default:
				echo '<tr' . ($element->bunched ? ' class="bunched"' : '') . '>' . chr(10);
				echo '<td' . ($cnt == 0 ? ' class="first"' : '') . '>';
				echo $element->render() . chr(10);
				if ((isset($element->warnings) ? $element->warnings : false)) {
					echo '<ul class="warnings">';
					foreach ($element->warnings as $warning) {
						echo '<li>' . $warning . '</li>';
					}
					echo '</ul>';
				}
				if ($element->errors) {
					echo '<ul class="errors">';
					foreach ($element->errors as $error) {
						echo '<li>' . $error . '</li>';
					}
					echo '</ul>';
				}
				echo '</td></tr>' . chr(10);
				$cnt++;
				break;
		}
		$element->rendered = true;
	}
	echo '</table>';
	if ($action === 'fieldset') {
		echo '</fieldset>';
		break;
	}
}
//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------
	}
	if ($action === 'end') {
		echo '</form>';
	}
?>